package com.second.service.impl;

import com.second.consts.OrderStatus;
import com.second.mapper.FGoodsMapper;
import com.second.mapper.FGoodsPictureMapper;
import com.second.mapper.IFOrdMapper;
import com.second.model.dto.pay.OrdDto;
import com.second.model.dto.order.ForderDto;
import com.second.model.pojo.FGoods;
import com.second.model.pojo.FGoodsPicture;
import com.second.model.pojo.FOrd;
import com.second.service.IFOrdService;
import com.second.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class FOrdServiceImpl implements IFOrdService {

    @Autowired
    private IFOrdMapper ordDao;

    @Autowired
    private FGoodsMapper goodsMapper;

    @Autowired
    private FGoodsPictureMapper goodsPictureMapper;
    /**
     * 全部未支付
     * @param userId
     * @return
     */
    @Override
    public List<ForderDto> SelectAllOrd(Integer userId) {

        List<FOrd> ford = ordDao.findByUserId(userId);
        List<ForderDto> ord1 = new ArrayList<>();
        Date date =  new Date();
        for (int i = 0; i < ford.size() ; i++) {
            //未支付的订单超过20分钟就是过期
            if (ford.get(i).getOrdState().equals(OrderStatus.Order_Not_Pay) && (Math.abs(ford.get(i).getOrdCreateDate().getTime() - date.getTime()) >= (20 * 60 * 1000))) {
                ford.get(i).setOrdState(OrderStatus.Order_Out_Date);
                ordDao.save(ford.get(i));
            }
        }
        for (int i = 0; i < ford.size() ; i++) {
            if (ford.get(i).getOrdState().equals(OrderStatus.Order_Not_Pay)) {
          Optional<FGoods> goods = goodsMapper.findById(ford.get(i).getGoodsId());
                FGoodsPicture goodsPicture = goodsPictureMapper.getByGoodsId(goods.get().getId());
                ForderDto ord = new ForderDto();
                ord.setId(ford.get(i).getId());
                ord.setShopId(ford.get(i).getShopId());
                ord.setGoodsId(ford.get(i).getGoodsId());
                ord.setGoodsname(goods.get().getGoodsName());
                ord.setOrdCode(ford.get(i).getOrdCode());
                ord.setOrdPrice(ford.get(i).getOrdPrice());
                ord.setOrdNum(ford.get(i).getOrdNum());
                ord.setOrdState(ford.get(i).getOrdState());
                ord.setUserId(ford.get(i).getUserId());
                ord.setOrdCreateDate(ford.get(i).getOrdCreateDate());
                ord.setGoodspic(goodsPicture.getUrl1());
                ord.setOrdComState(ford.get(i).getOperation());
                ord1.add(ord);
            }
        }
        return ord1;
    }

    /**
     * 全部已用
     * @param userId
     * @return
     */
    @Override
    public List<ForderDto> UsedOrd(Integer userId) {

        List<FOrd> ford = ordDao.findByUserId(userId);
        List<ForderDto> ord1 = new ArrayList<>();
        for (int i = 0; i < ford.size() ; i++) {
            if(ford.get(i).getOrdState().equals(OrderStatus.Order_Used)){
                Optional<FGoods> goods = goodsMapper.findById(ford.get(i).getGoodsId());
                FGoodsPicture goodsPicture = goodsPictureMapper.getByGoodsId(goods.get().getId());
                ForderDto ord = new ForderDto();
                ord.setId(ford.get(i).getId());
                ord.setShopId(ford.get(i).getShopId());
                ord.setGoodsId(ford.get(i).getGoodsId());
                ord.setOrdCode(ford.get(i).getOrdCode());
                ord.setOrdPrice(ford.get(i).getOrdPrice());
                ord.setOrdNum(ford.get(i).getOrdNum());
                ord.setOrdState(ford.get(i).getOrdState());
                ord.setUserId(ford.get(i).getUserId());
                ord.setOrdCreateDate(ford.get(i).getOrdCreateDate());
                ord.setGoodspic(goodsPicture.getUrl1());
                ord.setGoodsname(goods.get().getGoodsName());
                ord.setOrdComState(ford.get(i).getOperation());
                ord1.add(ord);
            }
        }
        Collections.sort(ord1, new Comparator<ForderDto>() {
            @Override
            public int compare(ForderDto o1, ForderDto o2) {
                return (int) (o2.getOrdCreateDate().getTime() - o1.getOrdCreateDate().getTime());
            }
        });
        return ord1;
    }

    /**
     * 过期
     * @param userId
     * @return
     */
    @Override
    public List<ForderDto> outdateOrd(Integer userId) {
        List<FOrd> ford = ordDao.findByUserId(userId);
        List<ForderDto> ord1 = new ArrayList<>();
        Date date = new Date();
        for (int i = 0; i < ford.size() ; i++) {

            //未支付的订单超过20分钟就是过期
            if (ford.get(i).getOrdState().equals(OrderStatus.Order_Not_Pay) && (Math.abs(ford.get(i).getOrdCreateDate().getTime() - date.getTime()) >= (20 * 60 * 1000))) {
                ford.get(i).setOrdState(OrderStatus.Order_Out_Date);
                ordDao.save(ford.get(i));
            }
            if (ford.get(i).getOrdState().equals(OrderStatus.Order_Out_Date)) {
                Optional<FGoods> goods = goodsMapper.findById(ford.get(i).getGoodsId());
                FGoodsPicture goodsPicture = goodsPictureMapper.getByGoodsId(goods.get().getId());
                ForderDto ord = new ForderDto();
                ord.setId(ford.get(i).getId());
                ord.setShopId(ford.get(i).getShopId());
                ord.setGoodsId(ford.get(i).getGoodsId());
                ord.setOrdCode(ford.get(i).getOrdCode());
                ord.setOrdPrice(ford.get(i).getOrdPrice());
                ord.setOrdNum(ford.get(i).getOrdNum());
                ord.setOrdState(ford.get(i).getOrdState());
                ord.setUserId(ford.get(i).getUserId());
                ord.setOrdCreateDate(ford.get(i).getOrdCreateDate());
                ord.setGoodspic(goodsPicture.getUrl1());
                ord.setGoodsname(goods.get().getGoodsName());
                ord.setOrdComState(ford.get(i).getOperation());
                ord1.add(ord);
            }
        }
        return ord1;
    }

    @Override
    public int countGoodSold(Integer goodId) {
        return ordDao.countByGoodsId(goodId);
    }


    /**
     * 取消订单
     * @param id
     * @return
     */
    @Override
    public Optional<FOrd> CencelOrd(Integer id) {
        Optional<FOrd> ford = ordDao.findById(id);
        ford.get().setOrdState(OrderStatus.Order_Out_Date);
        ordDao.save(ford.get());
        return ford;
    }



    //添加订单
    @Override
    public OrdDto save(Double ordPrice, Integer userId,Integer shopId,Integer goodsId) {
//        FOrd ord=ordDao.findByUserIdAndShopId(userId,shopId);
//        if(ord!=null){
        FOrd o = new FOrd();
        String code = UUID.randomUUID().toString().replaceAll("-","").hashCode()+ DateUtil.format(new Date(),"yyyyMMdd");
        o.setOrdCode(code);
        o.setGoodsId(goodsId);
        o.setOrdPrice(ordPrice);
        o.setOrdNum(1);
        o.setOrdState(OrderStatus.Order_Not_Pay);
        o.setOrdCreateDate(new Date());
        o.setUserId(userId);
        o.setShopId(shopId);
        o.setOperation(4);
        ordDao.save(o);
        OrdDto ord=new OrdDto();
        FGoods goods=goodsMapper.getById(1);
        ord.setOrdCode(o.getOrdCode());
        ord.setGoodsId(goodsId);
        ord.setOrdPrice(ordPrice);
        ord.setOrdNum(1);
        ord.setOrdState(OrderStatus.Order_Not_Pay);
        ord.setOrdCreateDate(new Date());
        ord.setUserId(userId);
        ord.setGoodsPicture(goods.getGoodsPicture());
        ord.setShopId(shopId);
        ord.setOperation(4);
        return ord;
//        }else
//            return null;

    }

    @Override
    public int findByOrdCodeAndOrderPrice(String orderCode,Integer orderPrice) {

        FOrd o=ordDao.findByOrdCodeAndOrdPrice(orderCode,orderPrice);
        if(o.getOrdState()==0){
            o.setOrdState(1);//确认支付...
            ordDao.flush();
            return 1;//成功的状态
        }
        return 0;//支付失败!

    }
    //根据订单号查询订单
    @Override
    public FOrd findByOrdCode(String orderCode) {
        FOrd o=ordDao.findByOrdCode(orderCode);
        return o;
    }

    @Override
    public int changeOrdUsed(String orderCode) {
        FOrd o=ordDao.findByOrdCode(orderCode);
        o.setOrdState(OrderStatus.Order_Used);
        ordDao.save(o);
        return 1;
    }

    @Override
    public int outTimeOrd(String ordNo) {
        FOrd ord = ordDao.findByOrdCode(ordNo);
        //已经支付过
        if (ord.getOrdState()==OrderStatus.Order_Used){
            return 1;
        }else {
            ord.setOrdState(OrderStatus.Order_Out_Date);
            ordDao.save(ord);
            return 0;
        }

    }


}
